//野外boss模块

'use strict'
let underscore = require('underscore')
let pushMsg = require('../nodeapp/pushMsg')
let attackHandle = require('../core/attackHandle')
let sceneMgr = require('../core/sceneMgr')
let mailDao = require('../dao/mailDao')
let createMonsterHandle = require('../core/createMonsterHandle')

class FieldBossComponent {
  constructor(opts, factor) {
    this.name = opts.name
    this.sceneId = opts.sceneId
    this.fightAttri = opts.fightAttri
    this.playersMap = new Map()
    this.reward = opts.reward
    this.timer = null
    this.fieldBoss = null
    this.initFieldBoss(factor)
    this.createTime = new Date().getTime()

  }

  //初始化怪物
  initFieldBoss(factor) {
    this.fieldBoss = createMonsterHandle.createFieldBoss({
      name: this.name,
      fightAttri: this.fightAttri
    })
    if (factor && factor > 0) {
      this.fieldBoss.fightAttri.maxHp += 500 * factor
      this.fieldBoss.fightAttri.hp = this.fieldBoss.fightAttri.maxHp
    }
    let self = this
    this.timer = setInterval(function() {
      let damageArray = self.statisticsDamage()
      pushMsg.fieldBossDamageRank(self.sceneId, {
        damageArray: damageArray,
        hpPer: Math.floor(self.fieldBoss.fightAttri.hp / self.fieldBoss.fightAttri.maxHp * 10000)
      })
    }, 10000)
  }

  statisticsDamage() {
    //统计玩家对boss的伤害
    let _damageArray = []
    for (let p of this.playersMap.entries()) {
      _damageArray.push([p[0], p[1].damage, p[1].name])
    }
    //Log(_damageArray)
    let damageArray = underscore.sortBy(_damageArray, function(damage) {
      return damage[1]
    });
    return damageArray
  }

  //攻击野外boss
  attackFieldBoss(player) {
    let info = this.playersMap.get(player._id)
    if (!info) {
      info = {
        damage: 0,
        name: player.name
      }
      this.playersMap.set(player._id, info)
    }

    let fightMsg = attackHandle.attackFieldBoss(player, this.fieldBoss)
    fightMsg.fieldBossDamage += info.damage
    info.damage = fightMsg.fieldBossDamage

    if (fightMsg.result === 1) {
      //野外boss被击杀
      clearInterval(this.timer) //清除定时器
      let damageArray = this.statisticsDamage()
        //推送野外boss死亡消息
      pushMsg.fieldBossKill(260002, {
        name: player.name
      })

      //根据伤害发放奖励
      this.grantReward(damageArray)
    }
    return fightMsg
  }

  //根据伤害发放奖励
  grantReward(damageArray) {
    let title = this.name + '击杀奖励'
    let content = this.name + '击杀奖励'
    let length = damageArray.length
    if (length > 0) {
      let max = length > 5 ? 5 : length
      for (let i = 0; i < max; i++) {
        let pid = damageArray[i][0]
        let items = []
        let item = [this.reward[0], 1]
        items.push(item)
        mailDao.newAndSave(pid, title, content, items, 0, 0, function(err) {
          pushMsg.newMail(pid, '你获得了击杀守护boss的奖励,奖励到请邮箱查看')
        })
      }
    }

    if (length > 5) {
      let max = length > 20 ? 20 : length
      for (let i = 5; i < max; i++) {
        let pid = damageArray[i][0]
        let items = []
        let item = [this.reward[1], 1]
        items.push(item)
        mailDao.newAndSave(pid, title, content, items, 0, 0, function(err) {
          pushMsg.newMail(pid, '你获得了击杀守护boss的奖励,奖励到请邮箱查看')
        })
      }
    }

    if (length > 20) {
      for (let i = 20; i < length; i++) {
        let pid = damageArray[i][0]
        let items = []
        let item = [this.reward[2], 1]
        items.push(item)
        mailDao.newAndSave(pid, title, content, items, 0, 0, function(err) {
          pushMsg.newMail(pid, '你获得了击杀守护boss的奖励,奖励到请邮箱查看')
        })
      }
    }
  }
}

module.exports = FieldBossComponent
